import { getSecond, uuid } from '../../utils/common'
import { _sendVerificationCode , _checkVerificationCode } from '../../utils/sendCode'
import { ManageAdmin , Account , Author , AuthorContact} from '../../models/'
import { CODE , SEND_TIME , TOKEN_TIME , VERIFICATION_CODE_TYPE , UID_KEY , _getCountLimit , PROGRESS_STATUS} from '../../config/config'
import { _getAccount , _bindPhone } from '../../utils/account' 

/**
 * @api {post} /manage_author/authorList 获取作者列表
 * @apiPermission 管理员
 * @apiVersion 1.0.0 
 * @apiName authorList
 * @apiGroup z_manage_author
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/manage_author/authorList'

 * @apiParam {String}  token                   管理员token
 * @apiParam {Integer}  page                   页码 1开始
 * @apiParam {Integer}  limit                  每页显示条数 默认50
 * @apiParam {Integer}  uid                    创建作者用户id
 *
 *
 * @apiSuccess {Integer}  count                总条数
 * @apiSuccess {Array}    lit                  作者集合 
 * @apiSuccess {Object}   author               作者信息
 * @apiSuccess {String}   id                   作者id
 * @apiSuccess {String}   uid                  创建作者用户id 
 * @apiSuccess {String}   name                 作者姓名
 * @apiSuccess {String}   idcard               作者身份证
 * @apiSuccess {String}   phone                作者手机
 * @apiSuccess {String}   country              作者国家
 * @apiSuccess {String}   province             作者省
 * @apiSuccess {String}   job                  作者工作
 * @apiSuccess {String}   education            作者学历
 * @apiSuccess {String}   ext                  拓展字段
 * @apiSuccess {String}   ctime                创建时间
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/manage_author/authorList
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 */
export async function authorList(ctx) {
    let page  	           = ctx.request.body.page
	const uid              = ctx.request.body.uid
	if(!page) {
		page               = 0
	}
	page                   = Number(page) - 1
	const countLimit       = _getCountLimit(ctx.request.body.limit)
	const where            = {status : 1}
	if(uid) {
		where.uid          = uid
	}
	const order			   = [['ctime', 'DESC']]
	const limit			   = [page * countLimit,countLimit]
	const query            = {where,order,limit}

	const authors          = await Author.findAll(query)
	const count            = await Author.count({where})

	ctx.body               = CODE.success({
		lit:authors,count
	})
}

/**
 * @api {post} /manage_author/workAuthor 获取作品关联作者信息
 * @apiPermission 管理员
 * @apiVersion 1.0.0 
 * @apiName workAuthor
 * @apiGroup z_manage_author
 * @apiExample 请求示例:
 * curl -H "Content-Type: application/json" -X POST -d '{}' 'http://copyright.hotchain.vip/api/manage_author/workAuthor'

 * @apiParam {String}  token                   管理员token
 * @apiParam {Integer}  contact_id             关联id
 *
 * @apiSuccess {Array}    authors              作者集合
 * @apiSuccess {Object}   author               作者信息
 * @apiSuccess {String}   id                   作者id
 * @apiSuccess {String}   uid                  创建作者用户id 
 * @apiSuccess {String}   name                 作者姓名
 * @apiSuccess {String}   idcard               作者身份证
 * @apiSuccess {String}   phone                作者手机
 * @apiSuccess {String}   country              作者国家
 * @apiSuccess {String}   province             作者省
 * @apiSuccess {String}   job                  作者工作
 * @apiSuccess {String}   education            作者学历
 * @apiSuccess {String}   ext                  拓展字段
 * @apiSuccess {String}   ctime                创建时间
 * 
 * @apiSampleRequest http://copyright.hotchain.vip/api/manage_author/workAuthor
 * @apiSuccessExample {json} 成功返回
 *     HTTP/1.1 200 OK
 *     {
    "code": 0
  }
 *
 * @apiUse ERROR
 */
export async function workAuthor(ctx) {
	const contact_id       = ctx.request.body.contact_id
	let author_id          
	const authorContact    = AuthorContact.findOne({where:{id:contact_id}})
	if(authorContact.type == 1) {
		author_id          = authorContact.author_id
	} else if (authorContact.type == 2) {
		author_id          = JSON.parse(authorContact.author_id)
	}
	const authors          = await Author.findOne({where : {id:author_id}})
	ctx.body               = CODE.success({authors})
}



export async function checkToken(ctx,next) {
	const token  = ctx.request.body.token
	const admin  = await ManageAdmin.findOne({where:{uid:token,status:1}})
	if(admin) {
		ctx.body = { admin }
		if(next) {
			return next()
		}
	}
	ctx.body     = CODE.MANAGE_ERROR.ERROR_ADMIN_NOT_FOUND
}
/**
 * @apiDefine ERROR
 * @apiErrorExample {json} 错误返回:
 *     {
 *       "code": "错误码",
 *       "error": "错误描述",
 *       "msg":"错误信息"
 *     }
 */